﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
    public partial class wfPerDereHabMnt : EditPage 
   {
        public const int _GRDLIST_STANULADO = 5;

      protected void Page_Load(object sender, EventArgs e)
      {
           if (this.IsPostBack){
               this.LoadInPostBack();
               return;}
           this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction) Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         this.SetInitialTitle("Personal");
      }

      public override void LoadListControls()
      { 
         ETablaGenDet objEDet = new ETablaGenDet();
         ETablaPEDet objEDetPE = new ETablaPEDet();
         ECCosto objECCosto = new ECCosto();
         EEmpEstable objEEmp = new EEmpEstable();
         EBanco objEBanco = new EBanco();

         IBOQuery objBOCCosto = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CCosto));
         IBOQuery objBOBanco = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.Banco));
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
         RHBOQry.ITablaPE objBOIPE = (RHBOQry.ITablaPE)WCFHelper.GetObject<RHBOQry.ITablaPE>(typeof(RHBOQry.TablaPE));
         SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));

         objEDet.IdTabla = ConstMultitabla.TIPO_DOCUMENTO;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTDocIden, dt, "dsAbrev", "idCodigo", true, true);
         WebHelper.LoadListControl(ddlidTipoDocDH, dt, "dsAbrev", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.SEXO ;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidSexo, dt, "dsDescrip", "idCodigo", true, true);
         WebHelper.LoadListControl(ddlidGeneroDH, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.TIPO_VIA;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTipoVia, dt, "dsDescrip", "idCodigo", true, true);
         WebHelper.LoadListControl(ddlidTipoVia2, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.TIPO_ZONA;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTipoZona, dt, "dsDescrip", "idCodigo", true, true);
         WebHelper.LoadListControl(ddlidTipoZona2, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.VINCULO_FAMILIAR;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidVincFamDH, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.DOC_VINCULO_FAMILIAR;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTipoDocVincFamDH, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.MOTIVO_BAJA_DH;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidMotivoBajaDH, dt, "dsDescrip", "idCodigo", true, true);

      }

      public override void DataToControls()
      {
         EPersonal objE = new EPersonal();
         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Personal));
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString() ;
         objE.IdPersonal = Request.QueryString["IdPersonal"];
         objE = (EPersonal)objBO.GetByKey(objE);

         if (objE != null)
         {

            WebHelper.EntityToControl(objE, "IdPersonal", txtidPersonal);
            WebHelper.EntityToControl(objE,"FeNacimien",txtfeNacimiento);
            WebHelper.EntityToControl(objE, "IdSexo", ddlidSexo);

            ECliProv objECli = new ECliProv();
            IBOQuery objBOCli = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CliProv));
            objECli.IdCliProv = objE.IdCliProv;
            objECli = (ECliProv)objBOCli.GetByKey(objECli);

            if (objECli != null)
            {
               WebHelper.EntityToControl(objECli,"IdCliProv",hdnidCliProv);
               WebHelper.EntityToControl(objECli,"DsAPaterno",txtdsAPaterno);
               WebHelper.EntityToControl(objECli,"DsAMaterno",txtdsAMaterno);
               WebHelper.EntityToControl(objECli,"DsNombres",txtdsNombres);
               WebHelper.EntityToControl(objECli,"IdTDocIden",ddlidTDocIden);
               WebHelper.EntityToControl(objECli, "NuDocIden", txtnuDocIden);

               WebHelper.EntityToControl(objECli,"NuTelf",txtnuTelf);
               WebHelper.EntityToControl(objECli,"NuFax",txtnufax);
               WebHelper.EntityToControl(objECli,"DsEmail",txtdsEmail);
               WebHelper.EntityToControl(objECli,"DsWeb", txtdsWeb);

            }
              
         }

         EPerDepen objEDet = new EPerDepen();
         RHBOQry.IPersonal objBODet = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
         objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objEDet.IdPersonal = Request.QueryString["IdPersonal"];
         DataTable dt = objBODet.GetByParentKeyPerDepen(objEDet);

         this.GridSetDataBinding(grdList, dt);

      }

      public override void RegisterGeneralScripts()
      { 
         txtidPersonal.Attributes.Add("readonly", "true");

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("var varRet = modalDialog('../MG/wfUbigeoBnd.aspx',725,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPais.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidDepa.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[2];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidProv.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[4];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidDist.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[6];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtdsUbigeo.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1] + ' / ' + varRet[3] + ' / ' + varRet[5] + ' / ' + varRet[7];");
         sb.AppendLine("ctl.title = ctl.value;");
         this.txtdsUbigeo.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         sb.AppendLine("var varRet = modalDialog('../MG/wfUbigeoBnd.aspx',725,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPais2.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidDepa2.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[2];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidProv2.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[4];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidDist2.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[6];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtdsUbigeo2.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1] + ' / ' + varRet[3] + ' / ' + varRet[5] + ' / ' + varRet[7];");
         sb.AppendLine("ctl.title = ctl.value;");
         this.txtdsUbigeo2.OnClientClick = sb.ToString();

         sb = new StringBuilder();


         sb.AppendLine("function validateEditForm(key){");
         sb.AppendLine("var varRpta = isEmptyControl('" + txtdsAPaterno.ClientID + "','Ingrese el Apellido Paterno');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + txtdsAPaterno.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + txtdsAMaterno.ClientID + "','Ingrese el Apellido Materno');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + txtdsAMaterno.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + txtdsNombres.ClientID + "','Ingrese los Nombres');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + txtdsNombres.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + ddlidTDocIden.ClientID + "','Seleccione el tipo de Documento de Identidad');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + ddlidTDocIden.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + txtnuDocIden.ClientID + "','Ingrese el numero de Documento de Identidad');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + txtnuDocIden.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + txtfeNacimiento.ClientID + "','Seleccione o ingrese la fecha de Nacimiento');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + txtfeNacimiento.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + ddlidSexo.ClientID + "','Seleccione el Genero');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + ddlidSexo.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("var varRpta = isEmptyControl('" + txtnuTelf.ClientID + "','Ingrese el numero de telefono');");
         sb.AppendLine("if(varRpta==true){");
         sb.AppendLine("document.getElementById('" + txtnuTelf.ClientID + "').focus();return !varRpta;}");
         sb.AppendLine("menu_doPostBack('"+mnuToolbar.ClientID+"','"+mnuToolbar.UniqueID+"',key);");
         sb.AppendLine("}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      public void RegisterEditPopupScripts()
      {
         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsApePaternoDH.ClientID + "','Ingrese el Apellido Paterno.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsApeMaternoDH.ClientID + "','Ingrese el Apellido Materno.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsNombresDH.ClientID + "','Ingrese el Apellido Paterno.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidTipoDocDH.ClientID + "','Seleccione el Tipo de Documento de Identidad.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuDocDH.ClientID + "','Ingrese el numero de Documento de Identidad.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeNacimientoDH.ClientID + "','Ingrese la fecha de nacimiento.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidGeneroDH.ClientID + "','Seleccione el Genero.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidVincFamDH.ClientID + "','Seleccione el vinculo familiar.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidTipoDocVincFamDH.ClientID + "','Seleccione el documento que acredite el vinculo Familiar.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuDocVincFamDH.ClientID + "','Ingrese el numero de documento que acredite el vinculo Familiar.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuDocVincFamDH.ClientID + "','Ingrese el numero de documento que acredite el vinculo Familiar.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("ctl = document.getElementById('" + this.ddlidVincFamDH.ClientID + "').value;");
         sb.AppendLine("if(ctl == '" + ConstVinculoFamiliar.GESTANTE + "'){");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeConcepDH.ClientID + "','Ingrese el mes/Año de Concepcion en caso de gestantes.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;}");
         if (this.GridEditAction == GridEditAction.Delete)
         {
            sb.AppendLine("varRpta = isEmptyControl('" + this.txtfeBajaDH.ClientID + "','Ingrese la fecha de baja.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidMotivoBajaDH.ClientID + "','Selecciona el motivo de baja.');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
         }

         sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsUbigeo.ClientID + "','Seleccione el Ubigeo.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidTipoVia.ClientID + "','Selecciona el Tipo de via.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsNombreVia.ClientID + "','Ingrese el nombre de la via.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuVia.ClientID + "','Ingrese el Numero de la direccion.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.ddlidTipoZona.ClientID + "','Selecciona el Tipo de zona.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtdsNombZona .ClientID + "','Ingrese el nombre de la zona.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("return true;}");
         
         sb.AppendLine("function onchange" + this.ddlidVincFamDH.ID + "(){");
         sb.AppendLine("var ddl = document.getElementById('" + this.ddlidVincFamDH.ClientID + "').value;");
         sb.AppendLine("var txt = document.getElementById('" + this.txtfeConcepDH.ClientID + "');");
         sb.AppendLine("if( ddl == '" + ConstVinculoFamiliar.GESTANTE + "')");
         sb.AppendLine("txt.disabled = false;");
         sb.AppendLine("else");
         sb.AppendLine("txt.disabled = true;");
         sb.AppendLine("};");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb.ToString(), true);

         this.bttOkPopup.OnClientClick ="return validate" + bttOkPopup.ID + "();";
         this.bttCancelPopup.OnClientClick =  "$find('" + this.mdlPopup.ClientID + "').hide();return false;";
         this.ddlidVincFamDH.Attributes.Add("onChange", "onchange" + this.ddlidVincFamDH.ID + "()");
      
      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if(e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if(this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break; 

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfPerDereHabiBnd.aspx");
               break;
      
         }
        
      }

      protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if(e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
                 this.GridEditAction = GridEditAction.AddNew;
                 this.lblTitlePopup.Text = "Derechohabientes (Nuevo)";
                 this.mdlPopup.Show();
                 this.LoadDataEditPopup(grdList, -1);
                 this.RegisterEditPopupScripts();
                 break;
         }
        
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
               this.lblTitlePopup.Text = "Derechohabientes (Editar)";
               this.GridEditAction = GridEditAction.Edit;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
            case ConstGridRowCommand.GRIDDELETE:
               this.lblTitlePopup.Text = "Derechohabientes (Dar de Baja)";
               this.GridEditAction = GridEditAction.Delete;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
         }
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            { e.Row.ForeColor = System.Drawing.Color.Red; }
         }


      }

      protected void bttOkPopup_Click(object sender, EventArgs e)
      {
         
           Object objResNuSec = null;
           int intResNusec = 0;
           DataTable dt= (DataTable)this.DataSource;
           DataRow dr= null;

           if(this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy )
           {
              objResNuSec = dt.Compute("MAX(nuSecuen)", "");
              if (objResNuSec == null || objResNuSec.ToString() == "")
                  intResNusec = 0;
              else
                  intResNusec = Convert.ToInt32(objResNuSec);
              intResNusec += 1;
              dr = dt.NewRow();
              dr["nuSecuen"] = intResNusec;
           }
           else
           {
             dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
           }

           if (this.GridEditAction != GridEditAction.Delete)
           {
              dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
              dr["idPersonal"] = Request.QueryString["IdPersonal"];
              dr["dsAPaterno"] = this.txtdsApePaternoDH.Text;
              dr["dsAMaterno"] = this.txtdsApeMaternoDH.Text;
              dr["dsNombres"] = this.txtdsNombresDH.Text;
              dr["dsApeNomb"] = this.txtdsApePaternoDH.Text + " " + this.txtdsApeMaternoDH.Text + " " + this.txtdsNombresDH.Text;
              dr["idTDocIden"] = this.ddlidTipoDocDH.SelectedItem.Value;
              dr["nuDocIden"] = this.txtnuDocDH.Text;
              dr["feNacimien"] = DateTime.Parse(this.txtfeNacimientoDH.Text);
              dr["idSexo"] = this.ddlidGeneroDH.SelectedItem.Value;
              dr["idVincFam"] = this.ddlidVincFamDH.SelectedItem.Value;
              dr["dsVincFam"] = this.ddlidVincFamDH.SelectedItem.Text;
              dr["idTipoDocAcre"] = this.ddlidTipoDocVincFamDH.SelectedItem.Value;
              dr["nuDocAcre"] = this.txtnuDocVincFamDH.Text;
              dr["nuMesConcep"] = this.txtfeConcepDH.Text.Replace("/","");
              dr["nuTlfCodLDN"] = this.txtnuCodLDN.Text;
              dr["nuTelf"] = this.txtnuTelefonoDH.Text;
              dr["dsEmail"] = this.txtdsEmailDH.Text;
              if (this.txtfeBajaDH.Text == "")
                 dr["feBaja"] = DBNull.Value;
              else
                 dr["feBaja"] = DateTime.Parse(this.txtfeBajaDH.Text);           
              dr["idMotivBaja"] = this.ddlidMotivoBajaDH.SelectedItem.Value;
              dr["dsUbigeo"] = this.txtdsUbigeo.Text;

              dr["idPais1"] = this.hdnidPais.Value;
              dr["idDepa1"] = this.hdnidDepa.Value;
              dr["idProv1"] = this.hdnidProv.Value;
              dr["idDist1"] = this.hdnidDist.Value;
              dr["idTipoVia1"] = this.ddlidTipoVia.SelectedItem.Value;
              dr["dsNombreVia1"] = this.txtdsNombreVia.Text;
              dr["nuVia1"] = this.txtnuVia.Text;
              dr["nuDepa1"] = this.txtnuDpto.Text;
              dr["nuInterior1"] = this.txtnuInterior.Text;
              dr["nuMz1"] = this.txtdsManzana.Text;
              dr["nuLote1"] = this.txtdsLote.Text;
              dr["nuKm1"] = this.txtdsKilometro.Text;
              dr["nuBlock1"] = this.txtdsBlock.Text;
              dr["dsEtapa1"] = this.txtdsEtapa.Text;
              dr["idTipoZona1"] = this.ddlidTipoZona.SelectedItem.Value;
              dr["dsNombZona1"] = this.txtdsNombZona.Text;
              dr["dsReferen1"] = this.txtdsReferen.Text;
              dr["dsUbigeo2"] = this.txtdsUbigeo2.Text;
              dr["idPais2"] = this.hdnidPais2.Value;
              dr["idDepa2"] = this.hdnidDepa2.Value;
              dr["idProv2"] = this.hdnidProv2.Value;
              dr["idDist2"] = this.hdnidDist2.Value;
              dr["idTipoVia2"] = this.ddlidTipoVia2.SelectedItem.Value;
              dr["dsNombreVia2"] = this.txtdsNombreVia2.Text;
              dr["nuVia2"] = this.txtnuVia2.Text;
              dr["nuDepa2"] = this.txtnuDpto2.Text;
              dr["nuInterior2"] = this.txtnuInterior2.Text;
              dr["nuMz2"] = this.txtdsManzana2.Text;
              dr["nuLote2"] = this.txtdsLote2.Text;
              dr["nuKm2"] = this.txtdsKilometro2.Text;
              dr["nuBlock2"] = this.txtdsBlock2.Text;
              dr["dsEtapa2"] = this.txtdsEtapa2.Text;
              dr["idTipoZona2"] = this.ddlidTipoZona2.SelectedItem.Value;
              dr["dsNombZona2"] = this.txtdsNombZona2.Text;
              dr["dsReferen2"] = this.txtdsReferen2.Text;
              if (this.rbDireccion1.Checked)
                 dr["stAsisEsSalud"] = ConstDirecReferEsSalud.DIRECCION_UNO;
              else
                 dr["stAsisEsSalud"] = ConstDirecReferEsSalud.DIRECCION_DOS;

              dr["stAnulado"] =  ConstFlagEstado.DESACTIVADO; 

           } 
           else
           {
              dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
           }

          if(this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
          {
             dt.Rows.Add(dr);
          }


          this.GridSetDataBinding(grdList, dt);
      }
       
      public override void LoadDataEditPopup(GridView grid, int rowIndex)
      {
          this.ClearEditPopup();
          this.LockControlsPopup();

          DataRow dr= this.GridGetRowFromDataKey(this.DataSource, grdList, rowIndex);

          if (dr == null)
              return;

          WebHelper.RowToControl(dr, "dsAPaterno", txtdsApePaternoDH);
          WebHelper.RowToControl(dr,"dsAMaterno",txtdsApeMaternoDH);
          WebHelper.RowToControl(dr,"dsNombres",txtdsNombresDH);
          WebHelper.RowToControl(dr,"idTDocIden",ddlidTipoDocDH);
          WebHelper.RowToControl(dr,"nuDocIden",txtnuDocDH);
          WebHelper.RowToControl(dr,"feNacimien",txtfeNacimientoDH);
          WebHelper.RowToControl(dr,"idSexo",ddlidGeneroDH);
          WebHelper.RowToControl(dr,"idVincFam",ddlidVincFamDH);
          WebHelper.RowToControl(dr,"dsVincFam",ddlidVincFamDH);
          WebHelper.RowToControl(dr,"idTipoDocAcre",ddlidTipoDocVincFamDH);
          WebHelper.RowToControl(dr,"nuDocAcre",txtnuDocVincFamDH);
          WebHelper.RowToControl(dr,"nuMesConcep",txtfeConcepDH);
          WebHelper.RowToControl(dr,"nuTlfCodLDN",txtnuCodLDN);
          WebHelper.RowToControl(dr,"nuTelf",txtnuTelefonoDH);
          WebHelper.RowToControl(dr,"dsEmail",txtdsEmailDH);
          WebHelper.RowToControl(dr,"feBaja",txtfeBajaDH);           
          WebHelper.RowToControl(dr,"idMotivBaja",ddlidMotivoBajaDH);
          WebHelper.RowToControl(dr,"dsUbigeo",txtdsUbigeo);
          WebHelper.RowToControl(dr,"idPais1",hdnidPais);
          WebHelper.RowToControl(dr,"idDepa1",hdnidDepa);
          WebHelper.RowToControl(dr,"idProv1",hdnidProv);
          WebHelper.RowToControl(dr,"idDist1",hdnidDist);
          WebHelper.RowToControl(dr,"idTipoVia1",ddlidTipoVia);
          WebHelper.RowToControl(dr,"dsNombreVia1",txtdsNombreVia);
          WebHelper.RowToControl(dr,"nuVia1",txtnuVia);
          WebHelper.RowToControl(dr,"nuDepa1",txtnuDpto);
          WebHelper.RowToControl(dr,"nuInterior1",txtnuInterior);
          WebHelper.RowToControl(dr,"nuMz1",txtdsManzana);
          WebHelper.RowToControl(dr,"nuLote1",txtdsLote);
          WebHelper.RowToControl(dr,"nuKm1",txtdsKilometro);
          WebHelper.RowToControl(dr,"nuBlock1",txtdsBlock);
          WebHelper.RowToControl(dr,"dsEtapa1",txtdsEtapa);
          WebHelper.RowToControl(dr,"idTipoZona1",ddlidTipoZona);
          WebHelper.RowToControl(dr,"dsNombZona1",txtdsNombZona);
          WebHelper.RowToControl(dr,"dsReferen1",txtdsReferen);
          WebHelper.RowToControl(dr,"dsUbigeo2",txtdsUbigeo2);
          WebHelper.RowToControl(dr,"idPais2",hdnidPais2);
          WebHelper.RowToControl(dr,"idDepa2",hdnidDepa2);
          WebHelper.RowToControl(dr,"idProv2",hdnidProv2);
          WebHelper.RowToControl(dr,"idDist2",hdnidDist2);
          WebHelper.RowToControl(dr,"idTipoVia2",ddlidTipoVia2);
          WebHelper.RowToControl(dr,"dsNombreVia2",txtdsNombreVia2);
          WebHelper.RowToControl(dr,"nuVia2",txtnuVia2);
          WebHelper.RowToControl(dr,"nuDepa2",txtnuDpto2);
          WebHelper.RowToControl(dr,"nuInterior2",txtnuInterior2);
          WebHelper.RowToControl(dr,"nuMz2",txtdsManzana2);
          WebHelper.RowToControl(dr,"nuLote2",txtdsLote2);
          WebHelper.RowToControl(dr,"nuKm2",txtdsKilometro2);
          WebHelper.RowToControl(dr,"nuBlock2",txtdsBlock2);
          WebHelper.RowToControl(dr,"dsEtapa2",txtdsEtapa2);
          WebHelper.RowToControl(dr,"idTipoZona2",ddlidTipoZona2);
          WebHelper.RowToControl(dr,"dsNombZona2",txtdsNombZona2);
          WebHelper.RowToControl(dr,"dsReferen2",txtdsReferen2);

          if (ConstDirecReferEsSalud.DIRECCION_UNO == dr["stAsisEsSalud"].ToString())
             this.rbDireccion1.Checked = true;
          else
             this.rbDireccion2.Checked = true;
      }
      
      public new void ClearEditPopup()
      {
          this.txtdsApePaternoDH.Text = "";
          this.txtdsApeMaternoDH.Text = "";
          this.txtdsNombresDH.Text = "";
          this.ddlidTipoDocDH.SelectedIndex = 0;
          this.txtnuDocDH.Text = "";
          this.txtfeNacimientoDH.Text = "";
          this.ddlidGeneroDH.SelectedIndex = 0;
          this.ddlidVincFamDH.SelectedIndex = 0;
          this.ddlidTipoDocVincFamDH.SelectedIndex = 0;
          this.txtnuDocVincFamDH.Text = "";
          this.txtfeConcepDH.Text = "";
          this.txtnuCodLDN.Text ="";
          this.txtnuTelefonoDH.Text = "";
          this.txtdsEmailDH.Text = "";
          this.txtfeBajaDH.Text = "";
          this.ddlidMotivoBajaDH.SelectedIndex = 0;
          this.txtdsUbigeo.Text = ""; 
          this.ddlidTipoVia.SelectedIndex = 0;
          this.txtdsNombreVia.Text = "";
          this.txtnuVia.Text = "";
          this.txtnuDpto.Text = "";
          this.txtnuInterior.Text = "";
          this.txtdsManzana.Text = "";
          this.txtdsLote.Text = "";
          this.txtdsKilometro.Text = "";
          this.txtdsBlock.Text = "";
          this.txtdsEtapa.Text = "";
          this.ddlidTipoZona.SelectedIndex = 0;
          this.txtdsNombZona.Text = "";
          this.txtdsReferen.Text = "";
          this.txtdsUbigeo2.Text = "";
          this.ddlidTipoVia2.SelectedIndex = 0;
          this.txtdsNombreVia2.Text = "";
          this.txtnuVia2.Text = "";
          this.txtnuDpto2.Text = "";
          this.txtnuInterior2.Text = "";
          this.txtdsManzana2.Text = "";
          this.txtdsLote2.Text = "";
          this.txtdsKilometro2.Text = "";
          this.txtdsBlock2.Text = "";
          this.txtdsEtapa2.Text = "";
          this.ddlidTipoZona2.SelectedIndex = 0;
          this.txtdsNombZona2.Text = "";
          this.txtdsReferen2.Text = ""; 

      }

      public void LockControlsPopup()
      {
         switch (this.GridEditAction)
          { 
              case GridEditAction.AddNew :
              case GridEditAction.Edit :
                  this.txtfeBajaDH.Enabled = false;
                  this.ddlidMotivoBajaDH.Enabled = false; 
                  break;
              case GridEditAction.Delete:
                  this.txtfeBajaDH.Enabled = true;
                  this.ddlidMotivoBajaDH.Enabled = true;
                  this.txtdsApePaternoDH.Enabled = false;
                  this.txtdsApeMaternoDH.Enabled = false;
                  this.txtdsNombresDH.Enabled = false;
                  this.ddlidTipoDocDH.Enabled = false;
                  this.txtnuDocDH.Enabled = false;
                  this.txtfeNacimientoDH.Enabled = false;
                  this.ddlidGeneroDH.Enabled = false;
                  this.ddlidVincFamDH.Enabled = false;
                  this.ddlidVincFamDH.Enabled = false;
                  this.ddlidTipoDocVincFamDH.Enabled = false;
                  this.txtnuDocVincFamDH.Enabled = false;
                  this.txtfeConcepDH.Enabled = false;
                  this.txtnuCodLDN.Enabled = false;
                  this.txtnuTelefonoDH.Enabled = false;
                  this.txtdsEmailDH.Enabled = false;
                  this.txtdsUbigeo.Enabled = false;
                  this.ddlidTipoVia.Enabled = false;
                  this.txtdsNombreVia.Enabled = false;
                  this.txtnuVia.Enabled = false;
                  this.txtnuDpto.Enabled = false;
                  this.txtnuInterior.Enabled = false;
                  this.txtdsManzana.Enabled = false;
                  this.txtdsLote.Enabled = false;
                  this.txtdsKilometro.Enabled = false;
                  this.txtdsBlock.Enabled = false;
                  this.txtdsEtapa.Enabled = false;
                  this.ddlidTipoZona.Enabled = false;
                  this.txtdsNombZona.Enabled = false;
                  this.txtdsReferen.Enabled = false;
                  this.txtdsUbigeo2.Enabled = false;
                  this.ddlidTipoVia2.Enabled = false;
                  this.txtdsNombreVia2.Enabled = false;
                  this.txtnuVia2.Enabled = false;
                  this.txtnuDpto2.Enabled = false;
                  this.txtnuInterior2.Enabled = false;
                  this.txtdsManzana2.Enabled = false;
                  this.txtdsLote2.Enabled = false;
                  this.txtdsKilometro2.Enabled = false;
                  this.txtdsBlock2.Enabled = false;
                  this.txtdsEtapa2.Enabled = false;
                  this.ddlidTipoZona2.Enabled = false;
                  this.txtdsNombZona2.Enabled = false;
                  this.txtdsReferen2.Enabled = false;
                  break;
          }
      }

      public override void SaveData(String key)
      {
           if (this.ValidateData() == false )
            return;

            IEntityBase objE= (IEntityBase) this.CreateEntitySet();
            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));

            String[] strRet = objPro.UpdateData(objE);
            String strMsg = "";

            switch (this.FormEditAction)
            { 
            
               case FormEditAction.AddNew:
               case FormEditAction.Copy:
                     strMsg = "Se ha registrado los derechohabientes : " + strRet[0].ToString() ;
                     break;
               default:
                     strMsg = "Se ha modificado los derechohabientes : " + txtidPersonal.Text;
                     break;
            }
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfPerDereHabiBnd.aspx");
      }

      public override void DeleteData()
      {
            IEntityBase objE= (IEntityBase)this.CreateEntitySet();
            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
            objPro.DeleteData(objE);

            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el derechohabiente  : " + txtidPersonal.Text, "wfPerDereHabiBnd.aspx");
       
      }

      public override Object CreateEntitySet()
      {

            DataTable dt= (DataTable)this.DataSource;
            this.ClearDeletedRow(this.DataSource, "stAnulado");
            IList<EPerDepen> colECerti = new List<EPerDepen>();

            if( this.FormEditAction == FormEditAction.Delete )
            {
                  foreach(DataRow dr in dt.Rows)
                  {
                     this.MarkDeletedRow(dr, "stAnulado");
                  }
            }
       

            for (int intTypeUpd = 0; intTypeUpd<=1; intTypeUpd++)
            {
               DataTable dtChange= null;
               if( intTypeUpd == 0 )
                  dtChange = dt.GetChanges(DataRowState.Added);
               else
                  dtChange = dt.GetChanges(DataRowState.Modified);
         

               if (dtChange != null )
               {
                  foreach(DataRow drDet in dtChange.Rows)
                  {
                     EPerDepen objEDet= DataHelper.CopyDataRowToEntity<EPerDepen>(drDet,typeof(EPerDepen));

                     if(drDet.RowState == DataRowState.Added)
                        objEDet.EntityState = EntityState.Added;
               
                     if(drDet.RowState == DataRowState.Modified)
                     {
                        if(objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                           objEDet.EntityState = EntityState.Modified;
                        else
                           objEDet.EntityState = EntityState.Deleted;
                     }

                     colECerti.Add(objEDet);
                  }
               }
            }
         
            EPersonalSet objESet = new EPersonalSet();
            objESet.ColPerDepen = colECerti; 

          return objESet;
      }
    
      public override void LockControls()
      {
         //if (this.FormEditAction == FormEditAction.Delete)
         //else  
      }

   }
}